package com.boot.filter;

import com.alibaba.fastjson.JSONObject;
import com.boot.common.MyHttpServletRequestWrapper;
import com.boot.entity.UserDTO;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author Chunsheng.Zhang
 * @date 2021/4/1 15:32
 * @description : TODO
 */
@WebFilter(filterName = "commonFilter", urlPatterns = "/test/*")
public class ParamFilter implements Filter {


    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {


        try {

            //复制request对象,requestWrapper代替了HttpServletRequest对象，不同之处就是requestWrapper在
            MyHttpServletRequestWrapper requestWrapper = new MyHttpServletRequestWrapper((HttpServletRequest) request);


            //通过request获取请求体中所有对象
            //HttpServletRequest req = (HttpServletRequest)request;
            BufferedReader streamReader = new BufferedReader( new InputStreamReader(requestWrapper.getInputStream(), "UTF-8"));
            StringBuilder responseStrBuilder = new StringBuilder();
            String inputStr;
            while ((inputStr = streamReader.readLine()) != null) {
                responseStrBuilder.append(inputStr);
            }
            UserDTO userDTO = JSONObject.parseObject(responseStrBuilder.toString(), UserDTO.class);

            filterChain.doFilter(requestWrapper, response);
        } catch (IOException e) {
            e.printStackTrace();
        }





    }
}
